﻿using System;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Common.Commands.FolderCommands;
using Dto.ApiRequests.FolderForms;
using NServiceBus;
using Sanelib.DataView;
using WebApp.ModelService;
using WebApp.Services;

namespace WebApp.ControllerApis
{
    public class FolderController  : SmartApiController
    {
        public IBus Bus { get; set; }
        private readonly IFolderModelService _folderModelService;

        public FolderController(IUserSession userSession, IMappingEngine mappingEngine, IFolderModelService folderModelService)
            : base(userSession, mappingEngine)
        {
            _folderModelService = folderModelService;
        }

        public HttpResponseMessage Get([FromUri] SearchSpecification form)
        {
            var response = _folderModelService.GetPageBySpecification(form);
            
            return Content(response);
        }

        public HttpResponseMessage Post(AddFolderForm form)
        {
            var command = Mapper.Map<AddFolderForm, AddFolder>(form);
            return ExecuteCommand(command);
        }

        public HttpResponseMessage Put(UpdateFolderForm form)
        {
            var command = Mapper.Map<UpdateFolderForm, UpdateFolder>(form);
            return ExecuteCommand(command);
        }
        
        public HttpResponseMessage Delete(Guid id)
        {
            var command = new DeleteFolder(id);
            return ExecuteCommand(command);            
        }   
    }
}
